const axios = require('axios').default;
const fs = require('fs');
const apiUrl = 'http://api.ttshitu.com/predict';
const username = 'guixuqi';
const password = 'guixuqi7310';
const typeid = '29';

async function cal_img_angle(imageFile) {
    let buff = fs.readFileSync(imageFile);
    let base64data = buff.toString('base64');
    return new Promise((resolve, reject) => {
        axios.post(apiUrl, {
            'username': username,
            'password': password,
            'typeid': typeid,
            'image': base64data
        }).then(function (response) {
            let d = response.data;
            if (d.success){
                // handle success
                let num = 0
                let angle = Number(d.data.result)
                if(angle > 0) {
                    num = Math.ceil(angle/45)
                }else {
                    num = -(Math.ceil(-angle/45))
                }
                console.log('图像倾斜角度：', angle)
                resolve(num)
            } else {
                console.log(d.message)
                reject(d.message)
            }
        });
    })
    
}


module.exports = { cal_img_angle }



